import app


class LoginApi:

    def __init__(self):
        # 获取图片验证码
        self.img_verify_code_url = app.BASE_URL + "/common/public/verifycode1/{}"
        # 获取短信验证码
        self.sms_code_url = app.BASE_URL + "/member/public/sendSms"
        # 注册
        self.reg_url = app.BASE_URL + "/member/public/reg"
        # 登录
        self.login_url = app.BASE_URL + "/member/public/login"

    # 获取图片验证码
    def get_img_verify_code(self, session, random):
        url = self.img_verify_code_url.format(random)
        return session.get(url)

    # 获取短信验证码
    def get_sms_code(self, session, phone, code):
        param = {
            "phone": phone,
            "imgVerifyCode": code,
            "type": "reg"
        }
        return session.post(self.sms_code_url, data=param)

    # 注册
    def reg(self, session, phone, password, verifycode, phone_code, dy_server, invite_phone):
        param = {
            "phone": phone,
            "password": password,
            "verifycode": verifycode,
            "phone_code": phone_code,
            "dy_server": dy_server,
        }
        if invite_phone is not None:
            param["invite_phone"] = invite_phone
        return session.post(self.reg_url, data=param)

    # 登录
    # keywords=18800011121
    # password=qqq123
    def login(self, session, keywords, password):
        param = {
            "keywords": keywords,
            "password": password,
        }
        return session.post(self.login_url, data=param)